/*
 * NodeDao.java
 *
 * Created on 7 de abril de 2006, 16:11
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package fmap.dao;

import fmap.DatosCompartidos;
import fmap.model.MapF;
import fmap.model.Node;
import fmap.model.SearchAdvancedModel;
import fmap.utils.IConstants;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Vector;

/**
 *
 * @author javi
 */
public class NodeDao
{
    
    /** Creates a new instance of NodeDao */
    public NodeDao ()
    {
    }
    
    
    public boolean tieneHijos ( int idNode , int idTable)
    {
        try
        {
            Connection conn = ConnectionDao.getConnection ();
            
//            String statement = "SELECT COUNT(idNode) FROM fmap.node"+idTable+"WHERE idParent = ?";
            
            PreparedStatement ps = conn.prepareStatement ( "" +
                    "SELECT COUNT(idNode) " +
                    "FROM fmap.node"+idTable+
                    " WHERE idParent = ?");
            
            ps.setInt ( 1, idNode );
            
            ResultSet rs = ps.executeQuery ();
            
            if ( rs.next () )
            {
                if ( rs.getInt (1) > 0 )    return true;
                else                        return false;
            }
            
            rs.close ();
            ps.close ();
//            conn.close ();
            
        }
        catch( Exception e)
        {
            DatosCompartidos.getJFrameMain ().setLblMsj ( IConstants.ERROR );
            return false;
//            System.out.println ( e.getMessage () + "ERROR: NodeDao : tieneHijos" );
        }
        
        return true;
    }
    
    
    /**
     * Selecciona todos los nodos para un mapa de ficheros un nodo padre y para una coleccion
     */
    public Vector selectNodes ( int idMapF, int idParent, int idTable )
    {
        Vector vNodes = new Vector ();
        
        try
        {
            Connection conn = ConnectionDao.getConnection ();
            PreparedStatement ps = conn.prepareStatement ( "" +
                    "SELECT idNode, idMapF, idParent, idCollection, name, descrip, icon, size, path, type, date, keyWord, idTable, idTableParent, idTableSub  " +
                    "FROM fmap.node"+idTable+
                    " WHERE idMapF=? AND idParent=? " +
                    "ORDER BY type");
            
            ps.setInt ( 1, idMapF );
            ps.setInt ( 2, idParent );
            
            ResultSet rs = ps.executeQuery ();
            
            while ( rs.next () )
            {
                
                vNodes.add (
                        new Node (
                        rs.getInt ("idNode"),
                        rs.getInt ("idMapF"),
                        rs.getInt ("idParent"),
                        rs.getInt ("idCollection"),
                        rs.getString ("name"),
                        rs.getString ("icon"),
                        rs.getLong ("size"),
                        rs.getString ("path"),
                        rs.getInt ("type"),
                        rs.getDate ("date"),
                        rs.getString ("keyWord"),
                        rs.getString ("descrip"),
                        rs.getInt ("idTable"),
                        rs.getInt ("idTableParent"),
                        rs.getInt ("idTableSub")
                        ));
                
            }
            
            rs.close ();
            ps.close ();
//            conn.close ();
            
        }
        catch( Exception e)
        {
            DatosCompartidos.getJFrameMain ().setLblMsj ( IConstants.ERROR );
            return null;
//            System.out.println ( e.getMessage () + "ERROR: NodeDao : selectNodeType" );
        }
        
        return vNodes;
    }
    
    
    /**
     * Selecciona los nodos para un mapa de ficheros un nodo padre una coleccion y un tipo dado
     */
    public Vector selectNodesType ( int idMapF, int idParent, int type, int idTable )
    {
        Vector vNodes = new Vector ();
        
        try
        {
            Connection conn = ConnectionDao.getConnection ();
            PreparedStatement ps = conn.prepareStatement ( "" +
                    "SELECT idNode, idMapF, idParent, idCollection, name, descrip, icon, size, path, type, date, keyWord, idTable, idTableParent, idTableSub  " +
                    "FROM fmap.node"+idTable+
                    " WHERE idMapF=? AND idParent=? AND type= ?");
            
            ps.setInt ( 1, idMapF );
            ps.setInt ( 2, idParent );
            ps.setInt ( 3, type );
            
            ResultSet rs = ps.executeQuery ();
            
            while ( rs.next () )
            {
                
                vNodes.add (
                        new Node (
                        rs.getInt ("idNode"),
                        rs.getInt ("idMapF"),
                        rs.getInt ("idParent"),
                        rs.getInt ("idCollection"),
                        rs.getString ("name"),
                        rs.getString ("icon"),
                        rs.getLong ("size"),
                        rs.getString ("path"),
                        rs.getInt ("type"),
                        rs.getDate ("date"),
                        rs.getString ("keyWord"),
                        rs.getString ("descrip"),
                        rs.getInt ("idTable"),
                        rs.getInt ("idTableParent"),
                        rs.getInt ("idTableSub")
                        ));
                
            }
            
            rs.close ();
            ps.close ();
//            conn.close ();
            
        }
        catch( Exception e)
        {
            DatosCompartidos.getJFrameMain ().setLblMsj ( IConstants.ERROR );
            return null;
//            System.out.println ( e.getMessage () + "ERROR: NodeDao : selectNodeType" );
        }
        
        return vNodes;
    }
    
    
    
    /**
     * Selecciona los nodos para un mapa de ficheros filtrando por el nombre y sin diferenciar entre mayusculas y minusculas
     */
    public Vector searchNodesName ( int idMapF, String name )
    {
        Vector vNodes = new Vector ();
        
        
        // Primero obtenemos los ids de las tablas en las que exista algun nodo de este mapa de ficheros
        Vector vIdsTable = new TableDao ().selectIdsTableMapF ( idMapF );
        
        
        try
        {
            if ( vIdsTable.size () > 0 )
            {
                Connection conn = ConnectionDao.getConnection ();
                
                for(int i=0; i<vIdsTable.size (); i++)
                {
                    
                    
                    PreparedStatement ps = conn.prepareStatement ( "" +
                            "SELECT idNode, idMapF, idParent, idCollection, name, descrip, icon, size, path, type, date, keyWord, idTable, idTableParent, idTableSub " +
                            "FROM fmap.node"+vIdsTable.get (i)+" " +
                            "WHERE idMapF=? AND UPPER(name) LIKE '%"+name.toUpperCase ()+"%'");
                    
                    ps.setInt ( 1, idMapF );
                    
                    ResultSet rs = ps.executeQuery ();
                    
                    while ( rs.next () )
                    {
                        
                        vNodes.add (
                                new Node (
                                rs.getInt ("idNode"),
                                rs.getInt ("idMapF"),
                                rs.getInt ("idParent"),
                                rs.getInt ("idCollection"),
                                rs.getString ("name"),
                                rs.getString ("icon"),
                                rs.getLong ("size"),
                                rs.getString ("path"),
                                rs.getInt ("type"),
                                rs.getDate ("date"),
                                rs.getString ("keyWord"),
                                rs.getString ("descrip"),
                                rs.getInt ("idTable"),
                                rs.getInt ("idTableParent"),
                                rs.getInt ("idTableSub")
                                ));
                        
                    }
                    
                    rs.close ();
                    ps.close ();
                }
//            conn.close ();
            }
            
        }
        catch( Exception e)
        {
            DatosCompartidos.getJFrameMain ().setLblMsj ( IConstants.ERROR );
            return null;
//            System.out.println ( e.getMessage () + "ERROR: NodeDao : searchNodesName" );
        }
        
        return vNodes;
    }
    
    
    
    
    
    /**
     * Selecciona los nodos para un mapa de ficheros filtrando por el nombre
     */
    public Vector searchNodesNameCase ( int idMapF, String name )
    {
        Vector vNodes = new Vector ();
        
        
        // Primero obtenemos los ids de las tablas en las que exista algun nodo de este mapa de ficheros
        Vector vIdsTable = new TableDao ().selectIdsTableMapF ( idMapF );
        
        try
        {
            if ( vIdsTable.size () > 0 )
            {
                Connection conn = ConnectionDao.getConnection ();
                
                for(int i=0; i<vIdsTable.size (); i++)
                {
                    
                    PreparedStatement ps = conn.prepareStatement ( "" +
                            "SELECT idNode, idMapF, idParent, idCollection, name, descrip, icon, size, path, type, date, keyWord, idTable, idTableParent, idTableSub " +
                            "FROM fmap.node"+vIdsTable.get (i)+" " +
                            "WHERE idMapF=? AND name LIKE '%"+name+"%'");
                    
                    ps.setInt ( 1, idMapF );
                    
                    ResultSet rs = ps.executeQuery ();
                    
                    while ( rs.next () )
                    {
                        
                        vNodes.add (
                                new Node (
                                rs.getInt ("idNode"),
                                rs.getInt ("idMapF"),
                                rs.getInt ("idParent"),
                                rs.getInt ("idCollection"),
                                rs.getString ("name"),
                                rs.getString ("icon"),
                                rs.getLong ("size"),
                                rs.getString ("path"),
                                rs.getInt ("type"),
                                rs.getDate ("date"),
                                rs.getString ("keyWord"),
                                rs.getString ("descrip"),
                                rs.getInt ("idTable"),
                                rs.getInt ("idTableParent"),
                                rs.getInt ("idTableSub")
                                ));
                        
                    }
                    
                    rs.close ();
                    ps.close ();
                }
//            conn.close ();
            }
            
        }
        catch( Exception e)
        {
            DatosCompartidos.getJFrameMain ().setLblMsj ( IConstants.ERROR );
            return null;
//            System.out.println ( e.getMessage () + "ERROR: NodeDao : searchNodesName" );
        }
        
        return vNodes;
    }
    
    
    
    
    /**
     * Busqueda avanzada de nodos
     */
    public Vector searchNodesAdvancedCase (int idMapF, SearchAdvancedModel searchAdvancedModel )
    {
        Vector vNodes = new Vector ();
        
        
//        String[] x = searchAdvancedModel.getAWordKey ();
        
        
        // Primero obtenemos los ids de las tablas en las que exista algun nodo de este mapa de ficheros
        Vector vIdsTable = new TableDao ().selectIdsTableMapF ( idMapF );
        
        
// Preparamos la consulta sql
        String sqlWhere = "";
        
        if( !searchAdvancedModel.getName ().equals ("") )
        {
            sqlWhere += " AND name LIKE '%"+searchAdvancedModel.getName ()+"%'";
        }
        
        if( searchAdvancedModel.getDateIniSql ()!= null  && searchAdvancedModel.getDateFinSql ()!= null )
        {
            sqlWhere += " AND date between '"+searchAdvancedModel.getDateIniSql ()+"' AND '"+searchAdvancedModel.getDateFinSql ()+"'";
        }
        else if( searchAdvancedModel.getDateIni () != null)
        {
            sqlWhere += " AND date > '"+searchAdvancedModel.getDateIniSql ()+"'";
        }
        else if( searchAdvancedModel.getDateFin () != null)
        {
            sqlWhere += " AND date < '"+searchAdvancedModel.getDateFinSql ()+"'";
        }
        
        if ( searchAdvancedModel.getSizeIni () > -1 )
        {
            sqlWhere += " AND size > "+searchAdvancedModel.getSizeIni ();
        }
        if ( searchAdvancedModel.getSizeFin () > -1 )
        {
            sqlWhere += " AND size < "+searchAdvancedModel.getSizeFin ();
        }
        
        if( !searchAdvancedModel.getWordKey ().equals (""))
        {
            sqlWhere += " AND keyWord LIKE '%"+searchAdvancedModel.getWordKey ()+"%'";
        }
        if( !searchAdvancedModel.getComment ().equals (""))
        {
            sqlWhere += " AND descrip LIKE '%"+searchAdvancedModel.getComment ()+"%'";
        }
        
        
        try
        {
            if ( vIdsTable.size () > 0 )
            {
                Connection conn = ConnectionDao.getConnection ();
                
                for(int i=0; i<vIdsTable.size (); i++)
                {
                    
                    PreparedStatement ps = conn.prepareStatement ( "" +
                            "SELECT idNode, idMapF, idParent, idCollection, name, descrip, icon, size, path, type, date, keyWord, idTable, idTableParent, idTableSub " +
                            "FROM fmap.node"+vIdsTable.get (i)+" " +
                            "WHERE idMapF=? "+sqlWhere);
                    
                    ps.setInt ( 1, idMapF );
                    
                    ResultSet rs = ps.executeQuery ();
                    
                    while ( rs.next () )
                    {
                        
                        vNodes.add (
                                new Node (
                                rs.getInt ("idNode"),
                                rs.getInt ("idMapF"),
                                rs.getInt ("idParent"),
                                rs.getInt ("idCollection"),
                                rs.getString ("name"),
                                rs.getString ("icon"),
                                rs.getLong ("size"),
                                rs.getString ("path"),
                                rs.getInt ("type"),
                                rs.getDate ("date"),
                                rs.getString ("keyWord"),
                                rs.getString ("descrip"),
                                rs.getInt ("idTable"),
                                rs.getInt ("idTableParent"),
                                rs.getInt ("idTableSub")
                                ));
                        
                    }
                    
                    rs.close ();
                    ps.close ();
                }
//            conn.close ();
            }
            
        }
        catch( Exception e)
        {
            DatosCompartidos.getJFrameMain ().setLblMsj ( IConstants.ERROR );
            return null;
//            System.out.println ( e.getMessage () + "ERROR: NodeDao : searchNodesName" );
        }
        
        return vNodes;
    }
    
    
    /**
     * Busqueda avanzada de nodos
     */
    public Vector searchNodesAdvanced (int idMapF, SearchAdvancedModel searchAdvancedModel )
    {
        Vector vNodes = new Vector ();
        
        
//        String[] x = searchAdvancedModel.getAWordKey ();
        
        
        // Primero obtenemos los ids de las tablas en las que exista algun nodo de este mapa de ficheros
        Vector vIdsTable = new TableDao ().selectIdsTableMapF ( idMapF );
        
        
// Preparamos la consulta sql
        String sqlWhere = "";
        
        if( !searchAdvancedModel.getName ().equals ("") )
        {
            sqlWhere += " AND UPPER(name) LIKE '%"+searchAdvancedModel.getName ().toUpperCase ()+"%'";
        }
        
        if( searchAdvancedModel.getDateIniSql ()!= null  && searchAdvancedModel.getDateFinSql ()!= null )
        {
            sqlWhere += " AND date between '"+searchAdvancedModel.getDateIniSql ()+"' AND '"+searchAdvancedModel.getDateFinSql ()+"'";
        }
        else if( searchAdvancedModel.getDateIni () != null)
        {
            sqlWhere += " AND date > '"+searchAdvancedModel.getDateIniSql ()+"'";
        }
        else if( searchAdvancedModel.getDateFin () != null)
        {
            sqlWhere += " AND date < '"+searchAdvancedModel.getDateFinSql ()+"'";
        }
        
        if ( searchAdvancedModel.getSizeIni () > -1 )
        {
            sqlWhere += " AND size > "+searchAdvancedModel.getSizeIni ();
        }
        if ( searchAdvancedModel.getSizeFin () > -1 )
        {
            sqlWhere += " AND size < "+searchAdvancedModel.getSizeFin ();
        }
        
        if( !searchAdvancedModel.getWordKey ().equals (""))
        {
            sqlWhere += " AND UPPER(keyWord) LIKE '%"+searchAdvancedModel.getWordKey ().toUpperCase ()+"%'";
        }
        if( !searchAdvancedModel.getComment ().equals (""))
        {
            sqlWhere += " AND UPPER(descrip) LIKE '%"+searchAdvancedModel.getComment ().toUpperCase ()+"%'";
        }
        
        
        try
        {
            if ( vIdsTable.size () > 0 )
            {
                Connection conn = ConnectionDao.getConnection ();
                
                for(int i=0; i<vIdsTable.size (); i++)
                {
                    
                    PreparedStatement ps = conn.prepareStatement ( "" +
                            "SELECT idNode, idMapF, idParent, idCollection, name, descrip, icon, size, path, type, date, keyWord, idTable, idTableParent, idTableSub " +
                            "FROM fmap.node"+vIdsTable.get (i)+" " +
                            "WHERE idMapF=? "+sqlWhere);
                    
                    ps.setInt ( 1, idMapF );
                    
                    ResultSet rs = ps.executeQuery ();
                    
                    while ( rs.next () )
                    {
                        
                        vNodes.add (
                                new Node (
                                rs.getInt ("idNode"),
                                rs.getInt ("idMapF"),
                                rs.getInt ("idParent"),
                                rs.getInt ("idCollection"),
                                rs.getString ("name"),
                                rs.getString ("icon"),
                                rs.getLong ("size"),
                                rs.getString ("path"),
                                rs.getInt ("type"),
                                rs.getDate ("date"),
                                rs.getString ("keyWord"),
                                rs.getString ("descrip"),
                                rs.getInt ("idTable"),
                                rs.getInt ("idTableParent"),
                                rs.getInt ("idTableSub")
                                ));
                        
                    }
                    
                    rs.close ();
                    ps.close ();
                }
//            conn.close ();
            }
            
        }
        catch( Exception e)
        {
            DatosCompartidos.getJFrameMain ().setLblMsj ( IConstants.ERROR );
            return null;
//            System.out.println ( e.getMessage () + "ERROR: NodeDao : searchNodesName" );
        }
        
        return vNodes;
    }
    
    
    
    /**
     * Selecciona el nodo padre del nodo (idParent) enviado como parametro
     */
    public Node selectNodeParent ( int idMapF, int idParent, int idTable)
    {
        Node n = null;
        try
        {
            Connection conn = ConnectionDao.getConnection ();
            
            PreparedStatement ps = conn.prepareStatement ( "" +
                    "SELECT idNode, idMapF, idParent, idCollection, name, descrip, icon, size, path, type, date, keyWord, idTable, idTableParent, idTableSub  " +
                    "FROM fmap.node"+idTable+
                    " WHERE idMapF=? AND idNode=?");
            
            ps.setInt ( 1, idMapF );
            ps.setInt ( 2, idParent );
            
            ResultSet rs = ps.executeQuery ();
            
            if ( rs.next () )
            {
                
                n = new Node (
                        rs.getInt ("idNode"),
                        rs.getInt ("idMapF"),
                        rs.getInt ("idParent"),
                        rs.getInt ("idCollection"),
                        rs.getString ("name"),
                        rs.getString ("icon"),
                        rs.getLong ("size"),
                        rs.getString ("path"),
                        rs.getInt ("type"),
                        rs.getDate ("date"),
                        rs.getString ("keyWord"),
                        rs.getString ("descrip"),
                        rs.getInt ("idTable"),
                        rs.getInt ("idTableParent"),
                        rs.getInt ("idTableSub")
                        );
                
            }
            
            rs.close ();
            ps.close ();
//            conn.close ();
            
        }
        catch( Exception e)
        {
            DatosCompartidos.getJFrameMain ().setLblMsj ( IConstants.ERROR );
            return null;
//            System.out.println ( e.getMessage () + "ERROR: NodeDao : selectNodeParent" );
        }
        
        return n;
    }
    
    
    
    public int selectMaxIdNode ( int idMapF, int idTable )
    {
        int idMaxNode = -1;
        
        try
        {
            Connection conn = ConnectionDao.getConnection ();
            PreparedStatement ps = conn.prepareStatement ( "SELECT MAX(idNode) FROM fmap.node"+idTable+" WHERE idMapF = ?" );
            
            ps.setInt ( 1, idMapF );
            
            ResultSet rs = ps.executeQuery ();
            
            if( rs.next () )
            {
                idMaxNode = rs.getInt (1);
            }
            
            
            rs.close ();
            ps.close ();
//            conn.close ();
            
        }
        catch( Exception e)
        {
            DatosCompartidos.getJFrameMain ().setLblMsj ( IConstants.ERROR );
            return idMaxNode;
//            System.out.println ( e.getMessage () + "ERROR: NodeDao : selectMaxIdNode" );
        }
        
        return idMaxNode;
        
    }
    
    
    
    /**
     * Selecciona los nodos para un mapa de ficheros un nodo padre una coleccion y un tipo dado
     */
    public Vector selectAllNodesNameResultSearchMapF ( int idMapF )
    {
        Vector vNodes = new Vector ();
        
        // Primero obtenemos los ids de las tablas en las que exista algun nodo de este mapa de ficheros
        Vector vIdsTable = new TableDao ().selectIdsTableMapF ( idMapF );
        
        try
        {
            if ( vIdsTable.size () > 0 )
            {
                
                Connection conn = ConnectionDao.getConnection ();
                
                for(int i=0; i<vIdsTable.size (); i++)
                {
                    
                    PreparedStatement ps = conn.prepareStatement ( "" +
                            "SELECT idNode, idMapF, idParent, idCollection, name, descrip, icon, size, path, type, date, keyWord, nameFound, idTable, idTableParent,idTableSub  " +
                            "FROM fmap.nodeResultSearch"+vIdsTable.get (i)+
                            " WHERE idMapF=? AND nameFound = ?");
                    
                    ps.setInt ( 1, idMapF );
                    ps.setString (2, "1");
                    
                    ResultSet rs = ps.executeQuery ();
                    
                    while ( rs.next () )
                    {
                        
                        vNodes.add (
                                new Node (
                                rs.getInt ("idNode"),
                                rs.getInt ("idMapF"),
                                rs.getInt ("idParent"),
                                rs.getInt ("idCollection"),
                                rs.getString ("name"),
                                rs.getString ("icon"),
                                rs.getLong ("size"),
                                rs.getString ("path"),
                                rs.getInt ("type"),
                                rs.getDate ("date"),
                                rs.getString ("keyWord"),
                                rs.getString ("descrip"),
                                rs.getString ("nameFound"),
                                rs.getInt ("idTable"),
                                rs.getInt ("idTableParent"),
                                rs.getInt ("idTableSub")
                                ));
                        
                    }
                    rs.close ();
                    ps.close ();
                }
                
//            conn.close ();
            }
        }
        catch( Exception e)
        {
            DatosCompartidos.getJFrameMain ().setLblMsj ( IConstants.ERROR );
            return null;
//            System.out.println ( e.getMessage () + "ERROR: NodeDao : selectNodesResultSearch" );
        }
        
        return vNodes;
    }
    
    
    /**
     * Selecciona los nodos para un mapa de ficheros un nodo padre una coleccion y un tipo dado
     */
    public Vector selectNodesResultSearch ( int idMapF, int idParent, int idTable )
    {
        Vector vNodes = new Vector ();
        
        try
        {
            Connection conn = ConnectionDao.getConnection ();
            PreparedStatement ps = conn.prepareStatement ( "" +
                    "SELECT idNode, idMapF, idParent, idCollection, name, descrip, icon, size, path, type, date, keyWord, nameFound, idTable, idTableParent,idTableSub  " +
                    "FROM fmap.nodeResultSearch"+idTable+
                    " WHERE idMapF=? AND idParent=?");
            
            ps.setInt ( 1, idMapF );
            ps.setInt ( 2, idParent );
            
            ResultSet rs = ps.executeQuery ();
            
            while ( rs.next () )
            {
                
                vNodes.add (
                        new Node (
                        rs.getInt ("idNode"),
                        rs.getInt ("idMapF"),
                        rs.getInt ("idParent"),
                        rs.getInt ("idCollection"),
                        rs.getString ("name"),
                        rs.getString ("icon"),
                        rs.getLong ("size"),
                        rs.getString ("path"),
                        rs.getInt ("type"),
                        rs.getDate ("date"),
                        rs.getString ("keyWord"),
                        rs.getString ("descrip"),
                        rs.getString ("nameFound"),
                        rs.getInt ("idTable"),
                        rs.getInt ("idTableParent"),
                        rs.getInt ("idTableSub")
                        ));
                
            }
            
            rs.close ();
            ps.close ();
//            conn.close ();
            
        }
        catch( Exception e)
        {
            DatosCompartidos.getJFrameMain ().setLblMsj ( IConstants.ERROR );
            return null;
//            System.out.println ( e.getMessage () + "ERROR: NodeDao : selectNodesResultSearch" );
        }
        
        return vNodes;
    }
    
    
    
    public Node insertNode ( Node node, int idTable )
    {
        
        try
        {
            Connection conn = ConnectionDao.getConnection ();
            
            PreparedStatement ps = conn.prepareStatement ( "" +
                    "INSERT INTO fmap.node"+idTable+" "+
                    "(idNode, idMapF, idParent, idCollection, name, size, path, type, date, idTable, idTableParent, idTableSub) " +
                    "VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
            
            ps.setInt (1, node.getIdNode () );
            ps.setInt (2, node.getIdMapF () );
            ps.setInt (3, node.getIdParent () );
            ps.setInt (4, node.getIdCollection () );
            ps.setString (5, node.getName () );
            ps.setLong (6, node.getSize () );
            ps.setString (7, node.getPath () );
            ps.setInt (8, node.getType () );
            ps.setDate (9, node.getDateSql ());
            ps.setInt (10, node.getIdTable () );
            ps.setInt (11, node.getIdTableParent ()  );
            ps.setInt (12, node.getIdTableSub () );
            
            
            ps.executeUpdate ();
            
            ps.close ();
//            conn.close ();
        }
        catch( Exception e)
        {
            DatosCompartidos.getJFrameMain ().setLblMsj ( IConstants.ERROR );
            return null;
            //System.out.println ( e.getMessage () );
        }
        
        return node;
    }
    
    
    public Node updateNode ( Node node, int idTable )
    {
        
        try
        {
            Connection conn = ConnectionDao.getConnection ();
            
            PreparedStatement ps = conn.prepareStatement ( "" +
                    "UPDATE fmap.node"+idTable+
                    " SET icon=?, descrip=?, keyWord=?" +
                    " WHERE idNode=? AND idMapF=?" );
            
            ps.setString (1, node.getIcon ());
            ps.setString (2, node.getDescrip ());
            ps.setString (3, node.getKeyWord ());
            
            ps.setInt (4, node.getIdNode ());
            ps.setInt (5, node.getIdMapF ());
            
            
            ps.executeUpdate ();
            
            ps.close ();
//            conn.close ();
        }
        catch( Exception e)
        {
            DatosCompartidos.getJFrameMain ().setLblMsj ( IConstants.ERROR );
            return null;
//            System.out.println ( e.getMessage () );
        }
        
        return node;
    }
    
    
    
    
    /**
     * Comprueba si existe el nodo que enviamos como parametro, y devuelve true si existe o false sino existe
     */
    public boolean existNodeResultSearch ( int idNode, int idMapF, int idTable )
    {
        boolean existe = false;
        try
        {
            Connection conn = ConnectionDao.getConnection ();
            PreparedStatement ps = conn.prepareStatement ( "" +
                    "SELECT idNode " +
                    "FROM fmap.nodeResultSearch"+idTable+
                    " WHERE idNode = ? AND idMapF = ?" +
                    " ORDER BY type");
            
            ps.setInt ( 1, idNode );
            ps.setInt ( 2, idMapF );
            
            ResultSet rs = ps.executeQuery ();
            
            if ( rs.next () )
            {
                existe = true;
            }
            
            rs.close ();
            ps.close ();
//            conn.close ();
            
        }
        catch( Exception e)
        {
            DatosCompartidos.getJFrameMain ().setLblMsj ( IConstants.ERROR );
            return false;
//            System.out.println ( e.getMessage () + "ERROR: NodeDao : existNodeResultSearch" );
        }
        
        return existe;
    }
    
    
    public Node insertNodeResultSearch ( Node node, String nameFound, int idTable )
    {
        
        try
        {
            Connection conn = ConnectionDao.getConnection ();
            
            PreparedStatement ps = conn.prepareStatement ( "" +
                    "INSERT INTO fmap.nodeResultSearch"+idTable+
                    " (idNode, idMapF, idParent, idCollection, name, size, path, type, date, descrip, keyWord, icon, nameFound, idTable, idTableParent, idTableSub) " +
                    " VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
            
            
            ps.setInt (1, node.getIdNode () );
            ps.setInt (2, node.getIdMapF () );
            ps.setInt (3, node.getIdParent () );
            ps.setInt (4, node.getIdCollection () );
            ps.setString (5, node.getName () );
            ps.setLong (6, node.getSize () );
            ps.setString (7, node.getPath () );
            ps.setInt (8, node.getType () );
            ps.setDate (9, node.getDateSql ());
            ps.setString (10, node.getDescrip ());
            ps.setString (11, node.getKeyWord ());
            ps.setString (12, node.getIcon ());
            ps.setString (13, nameFound);
            ps.setInt (14, node.getIdTable () );
            ps.setInt (15, node.getIdTableParent () );
            ps.setInt (16, node.getIdTableSub () );
            
            ps.executeUpdate ();
            
            ps.close ();
//            conn.close ();
        }
        catch( Exception e)
        {
            DatosCompartidos.getJFrameMain ().setLblMsj ( IConstants.ERROR );
            return null;
//            System.out.println ( e.getMessage () );
        }
        
        return node;
    }
    
    
    
    public boolean updateNodeResultSearch ( Node node, String nameFound, int idTable )
    {
        
        try
        {
            Connection conn = ConnectionDao.getConnection ();
            
            PreparedStatement ps = conn.prepareStatement ( "" +
                    "UPDATE fmap.nodeResultSearch"+idTable+" SET " +
                    "nameFound = ?" +
                    " WHERE idNode = ? AND idMapF = ?" );
            
            ps.setString (1, nameFound );
            ps.setInt (2, node.getIdNode () );
            ps.setInt (3, node.getIdMapF () );
            
            ps.executeUpdate ();
            
            ps.close ();
//            conn.close ();
        }
        catch( Exception e)
        {
            DatosCompartidos.getJFrameMain ().setLblMsj ( IConstants.ERROR );
            return false;
//            System.out.println ( e.getMessage () );
        }
        
        return true;
    }
    
    
    public boolean deleteNodeResultSearch ()
    {
        try
        {
            Connection conn = ConnectionDao.getConnection ();
            
            Vector vIdsTable = new TableDao ().selectIdsTable ();
            
            for( int i=0; i<vIdsTable.size (); i++ )
            {
                PreparedStatement ps = conn.prepareStatement ( "DELETE FROM fmap.nodeResultSearch"+vIdsTable.get (i) );
                ps.executeUpdate ();
                ps.close ();
            }
            
//            conn.close ();
        }
        catch( Exception e)
        {
            DatosCompartidos.getJFrameMain ().setLblMsj ( IConstants.ERROR );
            return false;
//            System.out.println ( e.getMessage () );
        }
        
        return true;
    }
    
    
    
    
    public boolean deleteNodeResultSearch ( int idMapF )
    {
        try
        {
            Connection conn = ConnectionDao.getConnection ();
            
            // Obtenemos las tablas en las que se almacenan los nodos de este mapa de ficheros
            Vector vIdsTable = new TableDao ().selectIdsTableMapF ( idMapF );
            
            for( int j=0; j<vIdsTable.size (); j++)
            {
                PreparedStatement ps = conn.prepareStatement ( "DELETE FROM fmap.nodeResultSearch"+vIdsTable.get (j)+" WHERE idMapF=?" );
                
                ps.setInt (1,idMapF);
                
                ps.executeUpdate ();
                
                ps.close ();
            }
//            conn.close ();
        }
        catch( Exception e)
        {
            DatosCompartidos.getJFrameMain ().setLblMsj ( IConstants.ERROR );
            return false;
//            System.out.println ( e.getMessage () );
        }
        
        return true;
    }
    
    
    public boolean deleteNodes ( int idMapF, int idTable )
    {
        
        try
        {
            Connection conn = ConnectionDao.getConnection ();
            
            PreparedStatement ps = conn.prepareStatement ( "DELETE FROM fmap.node"+idTable+" WHERE idMapF=?" );
            
            ps.setInt (1,idMapF);
            
            ps.executeUpdate ();
            
            ps.close ();
//            conn.close ();
        }
        catch( Exception e)
        {
            DatosCompartidos.getJFrameMain ().setLblMsj ( IConstants.ERROR );
            return false;
//            System.out.println ( e.getMessage () );
        }
        
        return true;
    }
    
    
    public boolean deleteNodeCollection ( int idCollection )
    {
        
        try
        {
            // Obtenemos los mapas de ficheros para esta coleccion
            
            MapF[] aMapF = new MapFDao ().selectMapFCollection ( idCollection );
            
            for( int i=0; i<aMapF.length; i++ )
            {
                // Obtenemos las tablas en las que se almacenan los nodos de este mapa de ficheros
                Vector vIdsTable = new TableDao ().selectIdsTableMapF ( aMapF[i].getIdMapF ());
                
                for( int j=0; j<vIdsTable.size (); j++)
                {
                    // Eliminamos los nodos de la tabla actual para el mapa de ficheros actual
                    this.deleteNodes ( aMapF[i].getIdMapF (), (Integer)vIdsTable.get (j));
                    
                }
                
            }
            
        }
        catch( Exception e)
        {
            DatosCompartidos.getJFrameMain ().setLblMsj ( IConstants.ERROR );
            return false;
//            System.out.println ( e.getMessage () );
        }
        
        return true;
    }
    
    
    public boolean deleteNodeMapF ( int idMapF )
    {
        
        try
        {
            // Obtenemos las tablas en las que se almacenan los nodos de este mapa de ficheros
            Vector vIdsTable = new TableDao ().selectIdsTableMapF ( idMapF );
            
            for( int j=0; j<vIdsTable.size (); j++)
            {
                // Eliminamos los nodos de la tabla actual para el mapa de ficheros actual
                this.deleteNodes ( idMapF, (Integer)vIdsTable.get (j));
            }
            
        }
        catch( Exception e)
        {
            DatosCompartidos.getJFrameMain ().setLblMsj ( IConstants.ERROR );
            return false;
//            System.out.println ( e.getMessage () );
        }
        
        return true;
    }
    
}
